package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.wx.dao.MemberExtractDao;
import com.bootdo.wx.dao.MemberPriceDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.MemberPriceDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.github.pagehelper.PageInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.wx.dao.OrderComDao;
import com.bootdo.wx.dao.OrderOrderDao;
import com.bootdo.wx.domain.OrderComDO;
import com.bootdo.wx.domain.OrderOrderDO;
import com.bootdo.wx.service.OrderComService;



@Service
public class OrderComServiceImpl implements OrderComService {
	
	 private final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private OrderComDao orderComDao;
	@Autowired
	private MemberExtractDao memberExtractDao;
	@Autowired
	private MemberPriceDao memberPriceDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private OrderOrderDao orderOrderDao;
	
	@Override
	public OrderComDO get(String id){
		return orderComDao.get(id);
	}
	
	@Override
	public List<OrderComDO> list(Map<String, Object> map){
		return orderComDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return orderComDao.count(map);
	}
	
	@Override
	public int save(OrderComDO orderCom){
		return orderComDao.save(orderCom);
	}
	
	@Override
	public int update(OrderComDO orderCom){
		return orderComDao.update(orderCom);
	}
	
	@Override
	public int remove(String id){
		return orderComDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return orderComDao.batchRemove(ids);
	}

	@Override
	public Map<String,Object> getMemberCom(Map<String, Object> map) {
		//提现总金额
		map.put("type","0");
		BigDecimal xPrice =  memberPriceDao.getSPriceByUser(map);   //消费  -
		map.put("type","1");
		BigDecimal tPrice =  memberPriceDao.getSPriceByUser(map);   //提现  -
		map.put("type","2");
		BigDecimal cPrice =  memberPriceDao.getSPriceByUser(map);   //充值  +
		map.put("type","3");
		BigDecimal fPrice =  memberPriceDao.getSPriceByUser(map);   //返利  +

		BigDecimal sPrice =  fPrice;   //收益
		BigDecimal allPrice =  cPrice.add(fPrice).subtract(xPrice).subtract(tPrice);   //余额
		//可提现余额
		BigDecimal canExPrice = new BigDecimal(0);
		if (xPrice.compareTo(cPrice) == 1) {  //消费大于充值
			canExPrice = fPrice.subtract(tPrice).subtract(xPrice.subtract(cPrice));
		} else {
			canExPrice = fPrice.subtract(tPrice);
		}

		Map<String,Object> priceMap = new HashMap<>();
		priceMap.put("extractMoney",tPrice);
		priceMap.put("allPrice",allPrice);
		priceMap.put("sPrice",sPrice);
		priceMap.put("canExPrice",canExPrice);
		return priceMap;
	}

	@Override
	public PageInfo<MemberPriceDO> getMemberComList(Map<String, Object> paramMap) {
		paramMap.put("delFlag","0");
		PageHelperUtils.page(paramMap);
		List<MemberPriceDO> list = memberPriceDao.list(paramMap);
		if(list.size()>0){
			for (MemberPriceDO memberPriceDO : list) {
				OrderOrderDO orderOrderDO = orderOrderDao.get(memberPriceDO.getGId());
				if(orderOrderDO!=null){
					MemberUserDO memberUserDO = memberUserDao.get(orderOrderDO.getUserId());
					if(memberUserDO!=null){
						memberPriceDO.setUserName(memberUserDO.getName());
						memberPriceDO.setUserAvator(memberUserDO.getAvator());
						memberPriceDO.setUserLevel(memberUserDO.getLevel()+"");
					}
				}
				
			}
		}
		PageInfo<MemberPriceDO> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public R getMemberAllPriceAndList(Map<String, Object> paramMap) {
		//获取所有直属下级
		PageHelperUtils.page(paramMap);
		paramMap.put("type", "1");
		List<Map<String, Object>> myFans = memberUserDao.getMyFans(paramMap);
		
		//获取总消费额
		paramMap.put("type", "3");
		List<Map<String, Object>> myFans1 = memberUserDao.getMyFans(paramMap);
		BigDecimal allMoney = new BigDecimal("0.00");
		for (Map<String, Object> map : myFans1) {
			map.put("userId", map.get("id").toString());
			BigDecimal b =orderOrderDao.countByUserAndMonth(map);
			allMoney=allMoney.add(b);
		}
		//获得本月每个下级团队的总消费金额
		/*paramMap.put("type", "1");
		List<Map<String, Object>> myFans2 = memberUserDao.getMyFans(paramMap);*/
		BigDecimal smllCountMoney = new BigDecimal("0.00");
		for (Map<String, Object> map : myFans) {
			//根据每一个直属下级获取每一个直属下级团队总额
			paramMap.put("type", "3");
			paramMap.put("userId", map.get("id").toString());
			BigDecimal countMoney = new BigDecimal("0.00");
			List<Map<String, Object>> myFans3 = memberUserDao.getMyFans(paramMap);
			for (Map<String, Object> map2 : myFans3) {
				map.put("type", "1");
				map.put("userId", map2.get("id").toString());
				BigDecimal b =orderOrderDao.countByUserAndMonth(map);
				/*map.put("money", b);*/
				countMoney=countMoney.add(b);
			}
			map.put("money", countMoney);
			smllCountMoney=smllCountMoney.add(countMoney);
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(myFans);
		Map<String, Object> map = new HashMap<>();
		log.error("allMoney:"+allMoney+"---"+"countMonuthMoney:"+smllCountMoney);
		map.put("allMoney", allMoney);
		map.put("myFans", pageInfo);
		map.put("countMonuthMoney", smllCountMoney);
		return R.ok().put("data", map);
	}

}
